package com.hudongwx.shansun.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hudongwx.shansun.R;
import com.hudongwx.shansun.utlis.API;
import com.hudongwx.shansun.utlis.MyJSONobject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class TitleActivity extends AppCompatActivity {
    private TextView tv;
    private String TAG = "TitleActivity";
    private ImageView iv;
    private ListView listview;
    private MyTitleAdapter myTitleAdapter;
    private String[] data;
    private JSONObject my;
    private String shareTitle,url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title);
        tv = (TextView) findViewById(R.id.title_tv);
        iv = (ImageView) findViewById(R.id.title_iv);
        listview = (ListView) findViewById(R.id.title_lv);

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        String parm = getIntent().getStringExtra("url");
        //把网址和标题分割开
        String[] data = parm.split("\\|");
        //这里就是网址
        String url=data[0];
        //这是标题
        tv.setText(data[1]);


        RequestParams params = new RequestParams(API.fragment_home_title(url));

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    my = new MyJSONobject(result);
                    Log.i(TAG, "onSuccess: " + my);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                myTitleAdapter = new MyTitleAdapter(my);
                listview.setAdapter(myTitleAdapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    class MyTitleAdapter extends BaseAdapter {
        private JSONObject data;

        public MyTitleAdapter(JSONObject data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return 30;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        public View loadFirst() {
            WebView web = null;
            try {
                String url = my.getJSONObject("brand").getString("brandUrl");
                web = new WebView(TitleActivity.this);
                web.loadUrl(url);
                Log.i(TAG, "loadFirst: ");

            } catch (JSONException e) {

            }
            return web;
        }

        @Override
        public long getItemId(int position) {
            try {
                JSONArray items = data.getJSONArray("data");
                return items.length() + 1;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (0 == position) {
                if (convertView != null && convertView instanceof WebView) {
                    return convertView;
                } else {
                    return loadFirst();
                }

            } else {
                try {
                    ImageView imageView = null;
                    if (convertView != null && convertView instanceof ImageView) {
                        imageView = (ImageView) convertView;
                    } else {
                        convertView = getLayoutInflater().inflate(R.layout.home_content_item, null);
                        imageView = (ImageView) convertView.findViewById(R.id.home_item_content_iv);
                    }

                    JSONObject item = my.getJSONArray("data").getJSONObject(position - 1);

                    x.image().bind(imageView, item.getString("imgUrl"));
                    return convertView;
                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
            return new TextView(TitleActivity.this);
        }
    }
}
